package com.example.tests;


import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;


import com.thoughtworks.xstream.XStream;


public class ContactDataGenerator {


        public static void main(String[] args) throws IOException {
                if (args.length < 3) {
                        System.out.println("Please specify parametres: <amout of test data> <file> <format>");
                        return;
                }
                
        int amount = Integer.parseInt(args[0]);
        File file = new File(args[1]);
        String format = args[2];
        
        if (file.exists()) {
                System.out.println("File exists, please remove it manually: " + file); 
        }
                
        List<ContactData> contacts = generateRandomContacts(amount);
        if ("csv".equals(format)) {
                saveContactsToCsvFile (contacts, file);
        } else if ("xml".equals(format)) {
                saveContactsToXmlFile (contacts, file);
        } else {
                System.out.println("Unknown format" + format);
                return;
        }
        }


        private static void saveContactsToXmlFile(List<ContactData> contacts, File file) throws IOException {
                XStream xstream = new XStream();
                xstream.alias("contact", ContactData.class);
                String xml = xstream.toXML(contacts);
                FileWriter writer = new FileWriter(file);
                writer.write(xml);
                writer.close(); 
        }
        
        public static List<ContactData> loadContactsFromXmlFile(File file) {
                XStream xstream = new XStream();
                xstream.alias("group", ContactData.class);
                return (List<ContactData>) xstream.fromXML(file);
                }


        private static void saveContactsToCsvFile(List<ContactData> contacts, File file) throws IOException {
                FileWriter writer = new FileWriter(file);
                for (ContactData contact : contacts) {
                        writer.write(contact.getFirstname() + "," + contact.getLastname() + "," + contact.getAddress1() + "," + contact.getHome1() + "," + contact.getMobile1() + "," + contact.getWork1() + "," + contact.getEmail() + "," + contact.getEmail2() + "," + contact.getBday() + "," + contact.getBmonth() + "," + contact.getByear() + "," + contact.getAddress2() + "," + contact.getHome2() + "\n");
                }
                writer.close();
                }


        public static List<ContactData> generateRandomContacts(int amount) {
                List<ContactData> list = new ArrayList<ContactData>();
                for (int i = 0; i < amount; i++) {
                        ContactData contact = new ContactData()
            .withFirstname(generateRandomString())
            .withLastname(generateRandomString())
            .withAddress1(generateRandomString())
            .withHome1(generateRandomString())
            .withMobile1(generateRandomString())
            .withWork1(generateRandomString())
            .withEmail1(generateRandomString())
            .withEmail2(generateRandomString())
            .withBday("-")
            .withBmonth("-")
            .withByear("-")
            .withAddress2(generateRandomString())
            .withHome2(generateRandomString());
                        list.add(contact);
                }
                return list;
        }
        
        public static String generateRandomString() {
                Random rnd = new Random();
                        if (rnd.nextInt(3) == 0) {
                                return "";
                        } else { 
                                return "test" +rnd.nextInt();
                        }
        }


}



